package com.example.teamtribe.main.seek;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.teamtribe.R;
import com.example.teamtribe.base.BaseFragment;
import com.example.teamtribe.common.UsersAdapter;
import com.example.teamtribe.event.Head;
import com.example.teamtribe.event.Refresh;
import com.example.teamtribe.event.SearchUser;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class PersonFragment extends BaseFragment {

    private UsersAdapter adapter;

    @Override
    protected int setContentView() {
        return R.layout.fragment_seek_rv;
    }

    @Override
    protected void onCreateView(View view) {
        RecyclerView recyclerView = view.findViewById(R.id.rv_seek);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setNestedScrollingEnabled(false);
        adapter = new UsersAdapter();
        recyclerView.setAdapter(adapter);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setHead(Head head) {
        adapter.notifyDataSetChanged();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFound(SearchUser searchUser) {
        adapter.add(searchUser.getUser());
        adapter.notifyDataSetChanged();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void clearData(Refresh refresh) {
        if(refresh.getType()==Refresh.SEEK){
            adapter.clear();
            adapter.notifyDataSetChanged();
        }
    }
}
